﻿using Core.Platform;
using UnityEngine;

namespace Core
{
    public class PlatformSystem : Component, IAwake, IPlatform
    {
        public IPlatform Platform { get; private set; }

        public bool IsEditor { get; private set; }
        public bool IsNative { get; private set; }
        public bool IsAndroid { get; private set; }
        public bool IsIPhone { get; private set; }
        public bool IsWeChat { get; private set; }

        public void Awake()
        {
            switch (Application.platform)
            {
                case RuntimePlatform.WindowsEditor:
                    Platform = G.Factory.Create<EditorPlatform>();
                    IsEditor = true;
                    break;
                case RuntimePlatform.Android:
                    Platform = G.Factory.Create<AndroidPlatform>();
                    IsNative = true;
                    IsAndroid = true;
                    break;
                case RuntimePlatform.IPhonePlayer:
                    Platform = G.Factory.Create<IOSPlatform>();
                    IsNative = true;
                    IsIPhone = true;
                    break;
                case RuntimePlatform.WebGLPlayer:
                    Platform = G.Factory.Create<WXWASMPlatform>();
                    IsWeChat = true;
                    break;
                default:
                    log.err($"目前尚不支持的平台 {Application.platform}");
                    break;
            }
        }

        public override void Dispose()
        {
            if (IsDisposed) return;

            Platform.Dispose();
            Platform = null;

            base.Dispose();
        }

        public void Exit()
        {
            if (IsDisposed) return;
            Platform.Exit();
        }

    }
}
